package com.example.mvvm.until.nullPrint;

import android.content.Context;
import android.text.TextUtils;

import com.example.mvvm.until.Tips;

import java.lang.reflect.Field;
import java.util.List;

public class NullUntil {
    public static boolean judgeObject(Context context, Object object) {
        Class aClass = object.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        if (declaredFields != null) {
            for (Field declaredField : declaredFields) {
                declaredField.setAccessible(true);
                NullTip annotation = declaredField.getAnnotation(NullTip.class);
                if (annotation != null) {
                    try {
                        Object o = declaredField.get(object);
                        if (o == null) {
                            Tips(context,annotation);
                            return false;
                        }
                        if (o instanceof String) {
                            if (TextUtils.isEmpty((CharSequence) o)) {
                                Tips(context,annotation);
                                return false;
                            }
                        }
                        if (o instanceof List) {
                            List tempList = (List) o;
                            if (tempList.size() == 0) {
                                Tips(context,annotation);
                                return false;
                            }
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return true;
    }

    public static void Tips(Context context, NullTip annotation) {
        if (!TextUtils.isEmpty(annotation.value())) {
            Tips.show(context, annotation.value());
        } else if(annotation.res()!=0) {
            Tips.show(context, context.getString(annotation.res()));
        }
    }
}
